package com.his.modules.sysjob.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.modules.business.entity.CaSignData;
import com.his.modules.business.service.CaSignDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component("caSynBodyAndSjcJob")
@Slf4j
public class CaSynBodyAndSjcJob {
    @Autowired
    private CaSignDataService caSignDataService;
    /**
     * CA同步Body和时间戳
     * @return
     */
    public void caSynBodyAndSjcJob() {
        log.info("CA获取Body原文及时间戳定时任务开始执行=================");
        List<String> fileList = new ArrayList<>();
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2023-08.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2023-09.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2023-10.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2023-11.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2023-12.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-01.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-02.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-0301到0314.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-15_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-16_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-17_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-18_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-19_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-20_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-21_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-22_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-23_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-24_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-25_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\2024-03-26_order.txt");
        QueryWrapper<CaSignData> queryWrapper = new QueryWrapper<>();
        int lineCount, totalUpdate;
        for(String file : fileList) {
            lineCount   = 0;
            totalUpdate = 0;
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                String line;
                String data[];
                while ((line = br.readLine()) != null) {
                    lineCount ++;
                    log.info("读取文件{}第{}行的行内容：{}", file, lineCount, line);
                    data = line.split("#");
                    log.info("分割数组长度：{}", data.length);
                    if(data.length == 5) {
                        queryWrapper.clear();
                        queryWrapper.eq("CA_ID", data[0]);
                        CaSignData caSignData = caSignDataService.getOne(queryWrapper);
                        if(!Objects.isNull(caSignData)) {
                            caSignData.setYwbody(data[2]);
                            caSignData.setQmsjc(data[3]);
                            boolean lb;
                            lb = caSignDataService.updateById(caSignData);
                            if (lb) totalUpdate ++;
                            log.info("CA_ID为{}的数据更新结果为{}", data[0], lb);
                        } else {
                            log.info("CA_ID为{}的数据未找到！", data[0]);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            log.info("文件{}共更新{}条！", file, totalUpdate);
        }
    }
}